// miniprogram/pages/me/me.js

Page({
  /**
   * 页面的初始数据
   */
  data: {
    headSrc: "",
    openId: "",
    name: "",
    cardId: "",
    phone: "",
    school: "",
    city: "点击选择城市",
    email: "",
    sexTypes: ["男", "女"],
    sexIndex: 0,
    sexType: "点击选择性别",
    // signTypes: ["个人报名", "机构报名"],
    // signIndex: 0,
    // signType: "个人报名",
    cardTypes: ["身份证", "护照"],
    cardIndex: 0,
    cardType: "点击选择证件类型",
    // input不可修改
    unchangable: false,

    // 正则表达式检测
    reName: false,
    reCardId: false,
    rePhone: false,
    reEmail: false,
    reSchool: false
  },

  // change function begin
  // changeSignPicker: function(e) {
  //   this.setData({
  //     signIndex: e.detail.value,
  //     signType: this.data.signTypes[this.data.signIndex]
  //   })
  // },

  changeName: function (e) {
    var pattern = /[\u4e00-\u9fa5a-zA-Z]+$/;
    if (pattern.test(e.detail.value)) {
      this.setData({
        name: e.detail.value,
        reName: true
      })
    } else {
      this.setData({
        reName: false
      })
      wx.showToast({
        title: '名称格式错误',
        image: "/images/unhappy.png"
      })
    }
  },

  changeSexPicker: function (e) {
    this.setData({
      sexIndex: e.detail.value,
    })
    // console.log(this.data.sexType, this.data.sexIndex, this.data.sexTypes[1])
    // output：男 1 女
    // 分开，先更新index再更新到性别，否则可能出错如上
    this.setData({
      sexType: this.data.sexTypes[this.data.sexIndex]
    })

  },

  changeCardPicker: function (e) {
    this.setData({
      cardIndex: e.detail.value,
    })
    // console.log(this.data.cardType, this.data.cardIndex, this.data.cardTypes[1])
    // output：身份证 1 护照
    // 分开，先更新index再更新到性别，否则可能出错如上
    this.setData({
      cardType: this.data.cardTypes[this.data.cardIndex]
    })
  },

  changeCardId: function (e) {
    // console.log(this.data.cardIndex)
    // 身份证ID
    if (this.data.cardIndex == 0) {
      var pattern = /\d{17}[0-9X]|\d{15}/;
      if (pattern.test(e.detail.value)) {
        this.setData({
          cardId: e.detail.value,
          reCardId: true
        })
      } else {
        this.setData({
          reCardId: false
        })
        wx.showToast({
          title: '身份证号格式错误',
          image: "/images/unhappy.png"
        })
      }
    } 
    // 护照ID
    else {
      var pattern1 = /^[a-zA-Z]{5,17}$/;
      var pattern2 = /^[a-zA-Z0-9]{5,17}$/;
      if (pattern1.test(e.detail.value) || pattern2.test(e.detail.value)) {
        this.setData({
          cardId: e.detail.value,
          reCardId: true
        })
      } else {
        this.setData({
          reCardId: false
        })
        wx.showToast({
          title: '护照号格式错误',
          image: "/images/unhappy.png"
        })
      }
    }
    
  },

  changePhone: function (e) {
    var pattern = /(13\d|14[579]|15[^4\D]|17[^49\D]|18\d)\d{8}/;
    if (pattern.test(e.detail.value)) {
      this.setData({
        phone: e.detail.value,
        rePhone: true
      })
    } else {
      this.setData({
        rePhone: false
      })
      wx.showToast({
        title: '电话格式错误',
        image: "/images/unhappy.png"
      })
    }
  },

  changeSchool: function (e) {
    var pattern = /[\u4e00-\u9fa5a-zA-Z]+$/;
    if (pattern.test(e.detail.value)) {
      this.setData({
        school: e.detail.value,
        reSchool: true
      })
    } else {
      this.setData({
        reSchool: false
      })
      wx.showToast({
        title: '学校格式错误',
        image: "/images/unhappy.png"
      })
    }
  },

  changeCityPicker: function (e) {
    this.setData({
      city: e.detail.value[1]
    })
  },

  changeEmail: function (e) {
    var pattern = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
    if (pattern.test(e.detail.value)) {
      this.setData({
        email: e.detail.value,
        reEmail: true
      })
    } else {
      this.setData({
        reEmail: false
      })
      wx.showToast({
        title: '邮箱格式错误',
        image: "/images/unhappy.png"
      })
    }
  },
  // change function end

  // 设置可修改
  changeInfo: function() {
    this.setData({
      unchangable: false
    })
  },

  // 保存信息
  saveInfo: function() {
    // 保存前检查格式
    if(this.data.reName == false) {
      wx.showToast({
        title: '名称格式错误',
        image: "/images/unhappy.png"
      })
      return;
    } else if (this.data.reCardId == false) {
      wx.showToast({
        title: '证件号格式错误',
        image: "/images/unhappy.png"
      })
      return;
    } else if (this.data.rePhone == false) {
      wx.showToast({
        title: '电话格式错误',
        image: "/images/unhappy.png"
      })
      return;
    } else if (this.data.reSchool == false) {
      wx.showToast({
        title: '学校格式错误',
        image: "/images/unhappy.png"
      })
      return;
    } else if (this.data.reEmail == false) {
      wx.showToast({
        title: '邮箱格式错误',
        image: "/images/unhappy.png"
      })
      return;
    }

    // 检查选择框
    if(this.data.city == "点击选择城市") {
      wx.showToast({
        title: '请选择城市',
        image: "/images/unhappy.png"
      })
      return;
    }

    if (this.data.sexType == "点击选择性别") {
      wx.showToast({
        title: '请选择性别',
        image: "/images/unhappy.png"
      })
      return;
    }

    if (this.data.cardType == "点击选择证件类型") {
      wx.showToast({
        title: '请选择证件类型',
        image: "/images/unhappy.png"
      })
      return;
    }


    // console.log(this.data.signType)
    // console.log(this.data.name)
    // console.log(this.data.sexType)
    // console.log(this.data.cardType)
    // console.log(this.data.cardId)
    // console.log(this.data.phone)
    // console.log(this.data.school)
    // console.log(this.data.city)
    // console.log(this.data.email)
    let that = this
    wx.cloud.callFunction({
      name: "addUser",
      data: {
        openId: that.data.openId,
        // signType: that.data.signTypes[that.data.signIndex],
        name: that.data.name,
        sexType: that.data.sexTypes[that.data.sexIndex],
        cardType: that.data.cardTypes[that.data.cardIndex],
        cardId: that.data.cardId,
        phone: that.data.phone,
        school: that.data.school,
        city: that.data.city,
        email: that.data.email
      },
      success: res => {
        console.log("调用云函数插入数据成功", res)
        that.setData({
          // 设置不可修改
          unchangable: true
        })
      },
      fail: res => {
        console.log("调用云函数插入数据失败", res)
      }
    })
    wx.showToast({
      title: '保存成功',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.headSrc, options.sex, options.openId)
    let that = this
    wx.cloud.callFunction({
      name: "getUserByOpenId",
      data: {
        openId: options.openId
      },
      success: res => {
        console.log("调用云函数获取用户信息成功", res)
        if(res.result.data.length != 0) {
          // console.log(res.result.data[0])
          that.setData({
            headSrc: options.headSrc,
            openId: res.result.data[0].openId,
            name: res.result.data[0].name,
            cardId: res.result.data[0].cardId,
            phone: res.result.data[0].phone,
            school: res.result.data[0].school,
            city: res.result.data[0].city,
            email: res.result.data[0].email,
            sexType: res.result.data[0].sex,
            // signType: res.result.data[0].signType,
            cardType: res.result.data[0].cardType,
            // 正则表达式检测
            reName: true,
            reCardId: true,
            rePhone: true,
            reSchool: true,
            reEmail: true,
            // 设置不可修改
            unchangable: true,
          })
        } else {
          if (options.sex == 0 || options.sex == 1) {
            that.setData({
              headSrc: options.headSrc,
              sexIndex: 0,
              openId: options.openId
            })
          } else {
            that.setData({
              headSrc: options.headSrc,
              sexIndex: 1,
              openId: options.openId
            })
          }
        }
        // console.log(that.data.user, "woxiaole")
      },
      fail: res => {
        console.log("调用云函数获取用户信息失败", res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})